package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;


import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    //依赖注入
    @Reference
    private UserService  userService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    /*
     * 删除用户
     * */
    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true, "删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }
    /*
     * 编辑用户
     * */
    @RequestMapping("edit")
    public Result edit(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds){
        try {
            //调用service中的更新方法
            userService.edit(user, roleIds);
            //更新成功, 返回更新成功的信息
            return new Result(true, "编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            //更新失败, 返回更新失败的信息
            return new Result(false, "编辑用户失败");
        }
    }
    /*
     * 根据用户ID获取选中的角色列表
     * */
    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer> roleList =  userService.findRoleIdsByUserId(id);
            return new Result(true,"获取成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取失败");
        }
    }

    /*
     * 根据ID获取用户数据
     * */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            com.itheima.health.pojo.User user = userService.findById(id);
            /*//调用日期工具类, 把数据库日期 转换为 字符串
            String orderDate = DateUtils.parseDate2String( user.getBirthday);
            user.setBirthday(orderDate);*/
            return new Result(true,"获取用户数据成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户数据失败");
        }
    }@RequestMapping("getMenuList")
    public Result getMenuList(String username){
        try {
             List<Menu> menuList = userService.getMenuList(username);

            return new Result(true,"获取用户数据成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户数据失败");
        }
    }
    /*
     * 添加用户
     * */
    @RequestMapping("add")
    public Result add(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds){
        try {
            String password = user.getPassword();
             password = passwordEncoder.encode(password);
             user.setPassword(password);
            //调用service中的添加方法
            userService.add(user, roleIds);
            //添加成功, 返回添加成功的信息
            return new Result(true, "用户添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败, 返回添加失败的信息
            return new Result(false, "用户添加失败");
        }
    }


    /*
     * 获取所有的角色
     * */
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Role> roleList = userService.findAll();
            return new Result(true,"获取所有角色成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取所有角色失败");
        }
    }


    /*
     * 分页查询
     * */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            //调用service中的分页查询方法
            PageResult pageResult = userService.pageQuery(queryPageBean);
            //查询成功, 返回查询成功的信息 及 数据
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息 及 数据
            return new PageResult(0L, null);
        }
    }

    /**
     * 获取用户名
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            //从授权框架上下文对象中 获取认证对象
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //再从 认证对象中 获取授权框架用户对象User
            User user = (User) authentication.getPrincipal();
            //用户对象User 获取 用户名
            String username = user.getUsername();
            //获取成功, 返回成功信息, 并返回用户名
            return new Result(true, MessageConst.ACTION_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
            //获取失败,返回失败信息
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 登录成功
     * @return
     */
    @RequestMapping("loginSuccess")
    public Result loginSuccess(){
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    /**
     * 登录失败
     * @return
     */
    @RequestMapping("loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }

    /**
     * 统一权限不足异常处理
     * @Author: Liuweipeng
     * @Param:
     * @Return:
     */
    @RequestMapping("accessDenied")
    public Result accessDenied() {
        return new Result(false, "权限不足,请更换账号或联系超级管理员!");
    }

    @RequestMapping("login")
    public Result login(String username, String password){
        System.out.println("oms backend====u:"+username+" ,p:"+password);
        if (userService.login(username, password)){
            System.out.println("login ok!!!");
            return new Result(true, MessageConst.ACTION_SUCCESS);
        } else {
            System.out.println("login fail");
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
